package com.baturu.offlineFord;

import com.baturu.ford.carvin.dal.DealCarTypeDAO;
import com.baturu.ford.carvin.dal.DealVinDAO;
import com.baturu.offlineFord.dto.PartDetailDTO;
import com.baturu.vin.ford.constants.TableConstants;
import com.baturu.vin.ford.dto.FordV2VinAnalyserResultDTO;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;

/**
 * @Author: chenjiahao
 * @Time: 2019/1/17
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
        "classpath*:config/spring-*-dataSource.xml",
        "classpath:spring-service.xml",
        "classpath*:spring-service-debugger.xml",})
public class DealVin{
    @Autowired
    private DealVinDAO vinDAO;

    @Autowired
    private DealCarTypeDAO carTypeDAO;
    /**
     * 把vin表数据预处理
     * @throws Exception
     */
    @Test
    public void dealVinInfo() throws Exception {
        Integer integer = vinDAO.queryMaxId();
        List<FordV2VinAnalyserResultDTO> list = Lists.newArrayList();
        for (int i = 1;i<=integer;i++){
            FordV2VinAnalyserResultDTO vinAnalyserResultDTO = vinDAO.queryV2ById(i);
            if (Objects.isNull(vinAnalyserResultDTO)) {
                continue;
            }
            String date = vinAnalyserResultDTO.getDate();
            List<String> dates = Splitter.on("\\").splitToList(date);
            if (dates.size() == TableConstants.DATE_LENGTH) {
                String moth = dates.get(1);
                String day = dates.get(0);
                if (moth.length()!=2) {
                    moth = "0"+ moth;
                }
                if (day.length()!=2) {
                    day = "0" + day;
                }
                date = dates.get(2) + moth + day;
            }
            vinAnalyserResultDTO.setDate(date);
            list.add(vinAnalyserResultDTO);
            if (list.size()%10000 == 0){
                vinDAO.updateVinByid(list);
                list.clear();
            }
        }
        if (!list.isEmpty()) {
            vinDAO.updateVinByid(list);
        }

    }

    /**
     * update ford_epc_vin
     set carBody = REPLACE(trim(`carBody`), CHAR(13), '') , ver = REPLACE(trim(`ver`), CHAR(13), '') ,
     `engine` = REPLACE(trim(`engine`), CHAR(13), '') ,
     transmission = REPLACE(trim(`transmission`), CHAR(13), '') ,
     driven = REPLACE(trim(`driven`), CHAR(13), '') ,
     carradio = REPLACE(trim(`carradio`), CHAR(13), '') ,
     displacement = REPLACE(trim(`displacement`), CHAR(13), '') ,
     airc = REPLACE(trim(`airc`), CHAR(13), '') ,
     area = REPLACE(trim(`area`), CHAR(13), '') ,
     carOilColor = REPLACE(trim(`carOilColor`), CHAR(13), '') ,
     enviroment = REPLACE(trim(`enviroment`), CHAR(13), '') ,
     trim = REPLACE(trim(`trim`), CHAR(13), '')
     where id < 1000
     * @throws Exception
     */
    @Test
    public void dealEnter() throws Exception {

    }

    @Test
    public void dealCarTypeDate() throws Exception {
        Integer maxId = carTypeDAO.queryMaxId();
        for (Integer i = 1; i <= maxId; i++) {
            String dBegin ;
            String dEnd ;
            Map<String,String> date = carTypeDAO.queryDate(i);
            if (MapUtils.isEmpty(date)) {
                continue;
            }
            String begin = date.get("begin");
            if (StringUtils.isNotBlank(begin)&&begin.length()>4) {
                List<String> d = Splitter.fixedLength(2).splitToList(begin);
                dBegin = d.get(2)+d.get(3)+d.get(1)+d.get(0);

            }else {
                dBegin = begin;
            }
            String end = date.get("end");
            if (StringUtils.isNotBlank(end)&&end.length()>4) {
                List<String> d = Splitter.fixedLength(2).splitToList(end);
                dEnd = d.get(2)+d.get(3)+d.get(1)+d.get(0);
            }else {
                dEnd = end;
            }
            carTypeDAO.update(dBegin,dEnd,i);
        }
        carTypeDAO.updateEnd();
        carTypeDAO.updateBegin();
    }

//    @Test
//    public void dealSubConditionTextCondition() throws Exception {
//        Integer maxId = carTypeDAO.queryMaxSubGroupId();
//        List<SubGroupDTO> list = Lists.newArrayList();
//        for (Integer i = 1; i <= maxId; i++) {
//            SubGroupDTO subGroupDTO = carTypeDAO.querySubGroup(i);
//            String textCondition = subGroupDTO.getTextCondition();
//
//            subGroupDTO.setDealTextCondition(dealTextCondition(textCondition));
//            list.add(subGroupDTO);
//        }
//        List<SubGroupDTO> list1 = Lists.newArrayList();
//        for (int i = 0; i <= list.size(); i++) {
//            list1.add(list.get(i));
//            if (list1.size() %10000 ==0) {
//                carTypeDAO.updateDealTextCondition(list1);
//                list.clear();
//            }
//        }
//    }

    private String dealTextCondition(String textCondition) {
        if (StringUtils.isBlank(textCondition)) {
            return "";
        }
        List<String> strings = Splitter.on("|").trimResults().splitToList(textCondition);
        StringBuilder sb = new StringBuilder();
        for (String string : strings) {
            List<String> s = Splitter.on(Pattern.compile("[ ~]")).trimResults().splitToList(string);
            for (String s1 : s) {
                String condition = carTypeDAO.queryIdx(s1);
                sb.append(condition).append(" ");
            }
            sb.append("|");
        }
        sb.deleteCharAt(sb.lastIndexOf(" ")).deleteCharAt(sb.lastIndexOf("|"));
        return sb.toString();
    }

    @Test
    public void dealPartsDetail() throws Exception {
        Integer maxId = vinDAO.queryMaxIdForPartsDetail();
        Integer start = 1733000;
        for (Integer i = 1743000; i <= maxId; i+=10000) {
            List<PartDetailDTO> partDetailDTO = vinDAO.queryPartsDetailById(start,i);
            partDetailDTO.forEach(d ->{
//                d.setDealPartsCode(d.getPartsCode().replaceAll("[ -]",""));
//                d.setDealBegin(dealDate(d.getBegin(),0));
//                d.setDealEnd(dealDate(d.getEnd(),1));
//                d.setDealTextCondition1(dealTextCondition(d.getTextCondition1()));
//                d.setDealTextCondition2(dealTextCondition(d.getTextCondition2()));
                d.setDealTextCondition3(dealTextCondition(d.getTextCondition3()));
            });
            vinDAO.updateDealPartsDetail(partDetailDTO);
            start = i;
        }
        List<PartDetailDTO> partDetailDTO = vinDAO.queryPartsDetailById(start,maxId);
        partDetailDTO.forEach(d ->{
//            d.setDealPartsCode(d.getPartsCode().replaceAll("[ -]",""));
//            d.setDealBegin(dealDate(d.getBegin(),0));
//            d.setDealEnd(dealDate(d.getEnd(),1));
//            d.setDealTextCondition1(dealTextCondition(d.getTextCondition1()));
//            d.setDealTextCondition2(dealTextCondition(d.getTextCondition2()));
            d.setDealTextCondition3(dealTextCondition(d.getTextCondition3()));
        });
        vinDAO.updateDealPartsDetail(partDetailDTO);
    }

    private String dealDate(String date, Integer i) {
        if (i==1&& StringUtils.isBlank(date)) {
            return "99999999";
        }
        if (StringUtils.isBlank(date)){
            return "";
        }
        List<String> dates = Splitter.on("/").splitToList(date);
        return dates.get(2)+dates.get(1)+dates.get(0);
    }

}
